以下是我下的語法,run之後出現string indices must be integers,不知道該怎樣解決,有去網路上爬文仍不知道該如何解決,想問問該如何修改。
import requests
import datetime
import json
timeout = 3
try:
resp = requests.get(url="https://www.taiwanstat.com/powers/latest/", verify=True, timeout=timeout)
if resp.status_code != 200:
print('invalid url:', resp.url)
else:
try:
_obj_json = resp.json()
with open('power.json', 'w', encoding='utf-8') as f:
json.dump(_obj_json,f, indent=4)
print(type(_obj_json))
_power_list = _obj_json
_my_power_list = []
for _power in _power_list:
_updateTime=_power['updateTime']
_eastSupply=_power['eastSupply']
_centerSupply=_power['centerSupply']
_northSupply=_power['northSupply']
_southSupply=_power['southSupply']
_southUsage=_power['southUsage']
_eastUseag=_power['eastUsage']
_northUsage=_power['northUsage']
_centerUsage=_power['centerUsage']
print("更新時間: {}, 東部及時發電量: {}, 中部及時發電量: {}, 北部及時發電量:{}, 南部及時發電量:{}, 南部及時用電量:{}, 東部及時用電量:{}, 北部及時用電量:{}, 中部及時用電量:{}".format(_updateTime, _eastSupply, _centerSupply, _northSupply, _southSupply, _southUsage, _eastUseag, _northUsage, _centerUsage))
_dbitem = [_updateTime, float(_eastSupply), float(_centerSupply), float(_northSupply), float(_southSupply), float(_southUsage), float(_eastUsage), float(_northUsage), float(_centerUsage)]
_my_stock_list.append(_dbitem)
#with open('twse_my_0520.json', 'w', encoding='utf-8') as f:
#json.dump(_my_stock_list, f, indent=4)
except ValueError as e:
print("JSON parsing Error")
except requests.HTTPError as e:
print("The server returned an HTTP error")